﻿using Common.Models;
using Core.Entities.SM;
using Core.Entities.Stock;
using Core.Interfaces;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Infrastructure.Services.SM
{
    /// <summary>
    /// 出库记录服务
    /// </summary>
    public class StockOutService : BaseService<StockOut, string>
    {
        private readonly IDapperRepository _dapperRepository;

        public StockOutService(
            IBaseRepository<StockOut, string> repository,
            IPagedRepository<StockOut, string> pagedRepository,
            IDapperRepository dapperRepository)
            : base(repository, pagedRepository)
        {
            _dapperRepository = dapperRepository;
        }

        /// <summary>
        /// 根据物料编码查询
        /// </summary>
        /// <param name="materCode">物料编码</param>
        /// <returns>出库记录列表</returns>
        public async Task<IEnumerable<StockOut>> GetByMaterCodeAsync(string materCode)
        {
            var filter = new QueryFilter
            {
                Conditions = new List<FilterCondition>
                {
                    new FilterCondition { Field = "MaterCode", Operator = "=", Value = materCode }
                }
            };

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result.Data;
        }

        /// <summary>
        /// 根据物料名称模糊查询
        /// </summary>
        /// <param name="materName">物料名称</param>
        /// <returns>出库记录列表</returns>
        public async Task<IEnumerable<StockOut>> GetByMaterNameAsync(string materName)
        {
            var filter = new QueryFilter
            {
                Conditions = new List<FilterCondition>
                {
                    new FilterCondition { Field = "MaterName", Operator = "like", Value = materName }
                }
            };

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result.Data;
        }

        /// <summary>
        /// 根据出库记录信息查询
        /// </summary>
        /// <returns>出库记录列表</returns>
        public async Task<PagedResult<StockOut>> GetByStockOutAsync(QueryFilter filter, StockOut stockOut)
        {
            // 初始化条件列表
            filter.Conditions = new List<FilterCondition>();

            // 添加物料编码模糊查询条件
            if (!string.IsNullOrEmpty(stockOut.MaterCode))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "MaterCode",
                    Operator = "like",
                    Value = stockOut.MaterCode
                });
            }

            // 添加物料名称模糊查询条件
            if (!string.IsNullOrEmpty(stockOut.MaterName))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "MaterName",
                    Operator = "like",
                    Value = stockOut.MaterName
                });
            }

            // 添加出库类型查询条件
            if (!string.IsNullOrEmpty(stockOut.OutType))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "OutType",
                    Operator = "=",
                    Value = stockOut.OutType
                });
            }

            // 添加出库批次号查询条件
            if (!string.IsNullOrEmpty(stockOut.OutBatchCode))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "OutBatchCode",
                    Operator = "like",
                    Value = stockOut.OutBatchCode
                });
            }

            // 添加仓库名称查询条件
            if (!string.IsNullOrEmpty(stockOut.WarehouseName))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "WarehouseName",
                    Operator = "like",
                    Value = stockOut.WarehouseName
                });
            }

            // 添加时间范围查询条件
            if (!string.IsNullOrEmpty(stockOut.OutTimeScope))
            {
                string[] dates = stockOut.OutTimeScope.Split('至');
                DateTime startDate = DateTime.Parse(dates[0].Trim());
                DateTime endDate = DateTime.Parse(dates[1].Trim());
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "OutTime",
                    Operator = ">=",
                    Value = startDate
                });
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "OutTime",
                    Operator = "<=",
                    Value = endDate
                });
            }

            // 始终添加未删除的条件
            filter.Conditions.Add(new FilterCondition
            {
                Field = "IsDeleted",
                Operator = "=",
                Value = false
            });

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result;
        }
    }
}